package com.enlorenz.common.security.des;

import com.enlorenz.common.security.MacGenerate;
import com.enlorenz.common.util.CodeUtils;

/**
 * 双倍DES
 * @author Karma
 *
 */
public class Coder4DoubleDES extends AbstractDes{
	{
		setDataType(EncodeTypeEnum.HEX);
		setKeyType(EncodeTypeEnum.HEX);
		setSecurityType(EncodeTypeEnum.HEX);
	}
	
	@Override
	protected String getDesTypeEnum() {
		return DesTypeEnum.DESDouble.name();
	}
	
	public byte[] encrypt(byte[] data, byte [] key) {  
		 return CodeUtils.hexString2Bytes(MacGenerate.DES_2(CodeUtils.bytes2HexString(data), 
	        		CodeUtils.bytes2HexString(key), 0));
    }
	
	 public byte[] decrypt(byte[] data, byte [] key){
        return CodeUtils.hexString2Bytes(MacGenerate.DES_2(CodeUtils.bytes2HexString(data), 
        		CodeUtils.bytes2HexString(key), 1));
    }
	
	/** 
     * 生成密钥 
     * @return 密钥 
     * @throws Exception 
     */  
    public String initKey(){  
    	byte [] buff=BaseDesUtils.initKey(null, DesTypeEnum.DES.name());
        return bytes2String(buff,getKeyType())+bytes2String(buff,getKeyType());  
    } 
	
}
